const input = require("scanline");
const connection = require("../db/connection");
const menu = require("../UI/menu");

async function add(){
    showPath();
    let sp = {
        "名称":"",
        "价格":0,
        "种类":"",
        "数量":0,
        "入库时间":0,
    };
    
    const zl = [
        "食物",
        "服装",
        "工具",
        "生活用品",
        "电子产品",
    ];
    
    sp.名称 = await input("请输入你的商品名称：");
    sp.价格 = parseFloat(await input("请输入你的商品价格："));
    
    const select = await menu(zl,"请选择你的商品种类：\n");
    sp.种类 = select.text;
    
    sp.数量 = parseInt(await input("请输入你的商品数量："));
    sp.入库时间 = parseInt(new Date().getTime()/1000);

    connection.query(`INSERT INTO product values(?,?,?,?,?)`, [
        sp["名称"],
        sp["价格"],
        sp["种类"],
        sp["数量"],
        sp["入库时间"],
    ], function(error){
        if(error){
            console.log("入库失败，请重试！");
            process.exit(1);
        }
        console.log(sp, "\033[33m商品已入库\033[0m");
    });
    connection.end();
}

module.exports = add;